package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class DGGAttr implements AdapterAttr {


    public static void main(String[] args) {
//        Map<String, Object> params1 = new TreeMap<>();
//        Map<String, Object> head = new HashMap<>();
//        head.put("content-type", "application/x-www-form-urlencoded");
//        params1.put("key", "FC72EEEF865B05F63106FC8C14D768D5");
//        params1.put("merchant_no", "youyouwangyou");
//        params1.put("gameid", 22);
//        params1.put("content-type", "application/x-www-form-urlencoded");
//        String response = HttpUtils.doPostForm("https://api.taohaoge.com/interfaces/getExpendAttribute.ashx", params1, head);

//        Map<String, String> params = new HashMap<>();
//        params.put("gameId", "22");
//        String response = HttpUtil.doGet("https://external.dg66.com/game/role", params, null);
        Map<String, String> params = new HashMap<>();
        params.put("gameId","1");
        String response = HttpRequest.post("https://external.dg66.com/getRole")
                .body(JSONObject.toJSONString(params))
                .execute().body();
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, String> params = new HashMap<>();
        String result = HttpRequest.post("https://external.dg66.com/gameList").execute().body();
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("gameName");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, String> params = new HashMap<>();
        params.put("gameId",gameId);
        String response = HttpRequest.post("https://external.dg66.com/getAreaList")
                .body(JSONObject.toJSONString(params))
                .execute().body();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setUuid(UUIDGenerator.generateUuid6());
            interServer.setId(datum.getString("areaId"));
            interServer.setName(datum.getString("areaName"));
            interServer.setUuid(UUIDGenerator.generateUuid6());
            String children = datum.getString("children");
            if(StringUtils.isNotEmpty(children)) {
                List<JSONObject> serverdata = JSONObject.parseArray(children, JSONObject.class);
                if (CollectionUtils.isNotEmpty(serverdata)) {
                    List<InterServerRes> interServerRes1 = new ArrayList<>();
                    for (JSONObject object : serverdata) {
                        InterServerRes interServer1 = new InterServerRes();
                        String id1 = object.getString("serverId");
                        String title1 = object.getString("serverName");
                        interServer1.setUuid(UUIDGenerator.generateUuid6());
                        interServer1.setId(id1);
                        interServer1.setName(title1);
                        interServer1.setUuid(UUIDGenerator.generateUuid6());
                        interServerRes1.add(interServer1);
                    }
                    interServer.setInterServerRes(interServerRes1);
                }
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        params.put("gameId",gameId);
        String response = HttpRequest.post("https://external.dg66.com/getProperty")
                .body(JSONObject.toJSONString(params))
                .execute().body();

        String roleresponse = HttpRequest.post("https://external.dg66.com/getRole")
                .body(JSONObject.toJSONString(params))
                .execute().body();

        JSONObject jsonObject = JSONObject.parseObject(response);
        JSONObject jsonObject2 = JSONObject.parseObject(roleresponse);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> data2 = JSONObject.parseArray(jsonObject2.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("1")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("2")).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(collect)) {
                collect = data2.stream().collect(Collectors.toList());
            }else {
                List<JSONObject> collect1 = data2.stream().collect(Collectors.toList());
                for(JSONObject jsonObject1 : collect1){
                    collect.add(jsonObject1);
                }
            }
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("3")||d.getString("type").equals("4")).collect(Collectors.toList());
        }
        if (collect.size()==0){
            InterAttrRes interAttrRes1 = new InterAttrRes();
            interAttrRes1.setId("无一键匹配多选属性");
            interAttrRes1.setName("无一键匹配多选属性");
            interAttrResList.add(interAttrRes1);
            return   interAttrResList;
        }
        for (JSONObject object : collect) {
            InterAttrRes interAttrRes = new InterAttrRes();
            String string = object.getString("children");
            String childProperty = object.getString("childProperty");

            JSONArray jsonArray = JSONArray.parseArray(string);
            JSONArray jsonArray2 = JSONArray.parseArray(childProperty);

            interAttrRes.setUuid(UUID.randomUUID().toString());
            String id = object.getString("game_key");
            if(StringUtils.isEmpty(id)){
                id = object.getString("id");
            }
            interAttrRes.setId(id);
            interAttrRes.setName(object.getString("name"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(jsonArray)){
                for(Object o : jsonArray) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UUID.randomUUID().toString());
                    interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("id"));
                    interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("name"));
                    interAttrResList1.add(interAttrRes1);
                }
            }
            if(CollectionUtils.isNotEmpty(jsonArray2)){
                for(Object o : jsonArray2) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UUID.randomUUID().toString());
                    interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("id"));
                    interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("name"));
                    interAttrResList1.add(interAttrRes1);
                }
            }
            interAttrRes.setInterServerRes(interAttrResList1);
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}